package modelo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ModeloIngredienteDAO extends Conexion {

	
	
	public ModeloIngredienteDAO() {
		super();
	}
	
	 public boolean registar(ModeloIngrediente m){
	        
	        boolean sw = false;
	        String tira="INSERT INTO Ingrediente (codigo, descripcion, stock, status) VALUES('"+m.getCodigo()+"','"+m.getDescripcion()+"',"+m.getStock()+",'"+m.getStatus()+"')";
	        sw= ejecutar(tira);
	        return sw;
	    }

	  public boolean actualizar(ModeloIngrediente m){
	     
	     boolean sw = false;
	     String tira="UPDATE Ingrediente SET stock ="+m.getStock()+" WHERE codigo='"+m.getCodigo()+"'  AND status = 'A'";
         sw= ejecutar(tira);
         return sw;
	 }

	  public boolean  consultar(ModeloIngrediente m){
	    
	    boolean sw = false;
	    String tira="SELECT * FROM Ingrediente WHERE codigo='"+m.getCodigo()+"' AND status='A'";
	    try {
	        ResultSet rs= consultar(tira);
	        if (rs.next()){
	            sw = true;
	            m.setDescripcion(rs.getString("descripcion"));
	            m.setCodigo(rs.getString("codigo"));
	            m.setStatus(rs.getString("status"));
	            m.setStock(rs.getFloat("stock"));
	         }
	    } catch (SQLException ex) {
	        Logger.getLogger(ModeloIngredienteDAO.class.getName()).log(Level.SEVERE, null, ex);
	    }
	    return sw;
	  }

	  public Vector<ModeloIngrediente> listar(){
	    Vector<ModeloIngrediente> v = new Vector<ModeloIngrediente>();
	    String tira="SELECT * FROM Ingrediente WHERE status='A'";
	    
	    try {
	        ResultSet rs= consultar(tira);
	        while (rs.next()){
	        	ModeloIngrediente  i = new ModeloIngrediente();
	            i.setDescripcion(rs.getString("descripcion"));
	            i.setCodigo(rs.getString("codigo"));
	            i.setStatus(rs.getString("status"));
	            i.setStock(rs.getFloat("stock"));
	            v.add(i);
	         }
	    } catch (SQLException ex) {
	        Logger.getLogger(ModeloIngredienteDAO.class.getName()).log(Level.SEVERE, null, ex);
	    }
	    
	    return v;
	  }


	  
	  public boolean revisarStock(ModeloIngrediente m,double porcion){
	      consultar(m);
	      if((m.getStock()-porcion)>=0)
	          return true;
	      else
	          return false;
	  }

	  public void restarStock(ModeloIngrediente m){
	       
	       String tira="UPDATE Ingrediente SET stock ="+m.getStock()+" WHERE codigo='"+m.getCodigo()+"'  AND status = 'A'";
	       ejecutar(tira);       
	  }

	
	  

	
}
